package com.registration.controller;

import com.registration.Annotation.OperateLog;
import com.registration.bean.DoctorBean;
import com.registration.bean.ScheduleBean;
import com.registration.service.DoctorService;
import com.registration.service.ScheduleService;
import com.registration.util.JsonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1")
@CrossOrigin
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 获取医生详情
     * 对应文档：GET /api/v1/doctor/{id}
     */
    @GetMapping("/doctor/{doctorId}")
    @OperateLog("获取医生详情")
    public JsonResponse getDoctorDetail(@PathVariable Integer doctorId) {
        DoctorBean doctor = doctorService.getDoctorById(doctorId);
        if (doctor == null) {
            return JsonResponse.fail("医生不存在");
        }
        return JsonResponse.success(doctor);
    }

    /**
     * 获取医生排班
     * 对应文档：GET /api/v1/doctor/{id}/schedule
     */
    @GetMapping("/doctor/{doctorId}/schedule")
    @OperateLog("获取医生排班")
    public JsonResponse getDoctorSchedules(@PathVariable Integer doctorId) {
        List<ScheduleBean> schedules = scheduleService.getFutureSchedulesByDoctorId(doctorId);
        return JsonResponse.success(schedules);
    }

    /**
     * 检查号源是否可用
     * 对应文档：GET /api/v1/schedule/{id}/available
     */
    @GetMapping("/schedule/{scheduleId}/available")
    @OperateLog("检查号源是否可用")
    public JsonResponse checkQuotaAvailable(@PathVariable Integer scheduleId) {
        ScheduleBean schedule = scheduleService.checkAndLockQuota(scheduleId);
        Map<String, Object> result = new HashMap<>();
        result.put("available", schedule != null);
        if (schedule != null) {
            result.put("remainingQuota", schedule.getRemaining_quota());
        }
        return JsonResponse.success(result);
    }
}